package com.xl.cloud.netty.nio;

import java.nio.ByteBuffer;

/**
 * @Author XiangLiang
 * @Since 2025/1/9 14:11
 * @Description: ReadOnlyBuffer类
 */
public class ReadOnlyBuffer {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(64);

        for (int i = 0; i < 64; i++) {
            byteBuffer.put((byte) i);
        }
        byteBuffer.flip();

        // 得到一个只读的缓冲区
        ByteBuffer readOnlyBuffer = byteBuffer.asReadOnlyBuffer();
        System.out.println(readOnlyBuffer.getClass());

        // 读取
        while (readOnlyBuffer.hasRemaining()) {
            System.out.println("---->\t" + readOnlyBuffer.get());
        }

        // 抛出ReadOnlyBufferException异常
        readOnlyBuffer.put((byte) 100);
    }
}
